package com.wj.springboot2demo.multids;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;

@Configuration
@MapperScan(basePackages = { "com.wj.springboot2demo.domain.ho" }, sqlSessionTemplateRef = "hoSqlSessionTemplate")
@ConfigurationProperties(prefix = "spring.second-datasource.druid")
public class HoDsConfiger extends AbstractDataSourceConfiger {

    @Bean(name = "hoDataSource")
    @ConfigurationProperties(prefix = "spring.second-datasource.druid")
    public DataSource hoDataSource() {
        DataSource dataSource = DruidDataSourceBuilder.create().build();
        return dataSource;
    }

    @Bean
    public SqlSessionFactory hoSqlSessionFactory(@Qualifier("hoDataSource") DataSource dataSource) throws Exception {
        return createSessionFactory(dataSource);
    }

    @Bean
    public SqlSessionTemplate hoSqlSessionTemplate(
            @Qualifier("hoSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory); // 使用上面配置的Factory
        return template;
    }

}
